home *** CD-ROM | disk | FTP | other *** search
- /*
- File: MemDebgM.cpp
-
- Contains: Mac-specific memory management debug routines
-
- Owned by: Jens Alfke, Vincent Lo, Nick Pilch, Michael Burbidge
-
- Copyright: © 1993 - 1995 by Apple Computer, Inc., all rights reserved.
-
- Change History (most recent first):
-
- <5> 5/4/95 jpa Abstracted out highest/lowest possible
- address stuff [1246077]
- <4> 1/12/95 jpa Include LowMem.h [1210936]
- <3> 10/24/94 jpa Call StripAddress in BasicValidatePtr.
- [1193659]
- <2> 9/29/94 RA 1189812: Mods for 68K build.
- <1> 9/14/94 jpa first checked in
-
- In Progress:
- */
-
-
- #ifndef _MEMCNFIG_
- #include "MemCnfig.h"
- #endif
-
-
- #if MM_DEBUG
-
-
- #ifndef _MEMDEBG_
- #include "MemDebg.h"
- #endif
-
- #ifndef _MEMMGRPV_
- #include "MemMgrPv.h"
- #endif
-
- #ifndef _MEMHOOKS_
- #include "MemHooks.h"
- #endif
-
- #ifndef __MEMORY__
- #include <Memory.h> // Mac memory manager
- #endif
-
- #ifndef __LOWMEM__
- #include <LowMem.h>
- #endif
-
-
- const size_t kMaxHandleBlockSize = 0x7FFFFFFF;
-
-
- //------------------------------------------------------------------------------
- // PtrInHeap
- //------------------------------------------------------------------------------
-
- inline static MMBoolean
- PtrInHeap( const void *p, THz heap )
- {
- return (p >= &heap->heapData) && (p < heap->bkLim);
- }
-
-
- //------------------------------------------------------------------------------
- // BasicValidatePtr
- //------------------------------------------------------------------------------
-
- const char*
- BasicValidatePtr( const void *p, MMBoolean mustBeInHeap )
- {
- // If we could be sure that p were in the app zone, we could be more
- // stringent. But it might be in the System heap or in temp-mem.
-
- #if 0
- static THz tempZone = kMMNULL;
- if (tempZone == kMMNULL)
- {
- // Find the temp zone by allocating a handle in it and then getting its zone:
- OSErr err;
-
- Handle handle = ::TempNewHandle(2, &err);
- MM_ASSERT(err == noErr && handle != kMMNULL);
-
- tempZone = ::HandleZone(handle);
- MM_ASSERT(tempZone != kMMNULL);
-
- ::DisposeHandle(handle);
- }
- #endif
-
- p = StripAddress((void*)p);
-
- if( p == kMMNULL )
- return "is NULL";
- // else if( (MMULong)p & 1 )
- // return "is odd";
- else if( (MMULong)p & 2 )
- return "isn't 4-byte-aligned";
- #if 0
- // This does not appear to work any longer with the Modern Memory Manager...
- else if( mustBeInHeap && !PtrInHeap(p,ApplicationZone())
- && !PtrInHeap(p,SystemZone())
- && !PtrInHeap(p,tempZone) )
- return "is not in a known Mac heap zone";
- #endif
- else if( /*!mustBeInHeap &&*/ (p<LOWEST_POSSIBLE_ADDRESS() || p>HIGHEST_POSSIBLE_ADDRESS()) )
- return "is outside the known universe";
- else
- return kMMNULL;
- }
-
-
- //------------------------------------------------------------------------------
- // MMValidateHandle
- //------------------------------------------------------------------------------
-
- MMBoolean
- MMValidateHandle( MMHandle MMHandle )
- {
- Handle h = (Handle) MMHandle;
-
- // Check handle itself as a pointer:
- const char *err = BasicValidatePtr(h);
-
- if( err == kMMNULL && *h == kMMNULL )
- err = "is purged";
-
- if( err ) {
- MM_WARN("Handle %p %s!",h,err);
- return kMMFalse;
- }
-
- // Check master pointer for validity:
- err = BasicValidatePtr(*h);
- if( err ) {
- MM_WARN("Handle %p's master %p %s!",h,*h,err);
- return kMMFalse;
- }
-
- // Check handle's size:
- size_t size = GetHandleSize(h);
- if( size > kMaxHandleBlockSize )
- MM_WARN("Handle %p has bogus size %08lx!",h,size);
- else if( MemError() != noErr )
- MM_WARN("Handle %p caused err %d on GetHandleSize",h,MemError());
- else
- return kMMTrue;
- return kMMFalse;
- }
-
- #endif /*MM_DEBUG*/